package com.cencat.driver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cencat.driver.entity.Driver;
import com.cencat.driver.mapper.DriverMapper;
import com.cencat.driver.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 司机服务实现类
 * 
 * @author cencat
 * @version 1.0
 * @since 2024-12-19
 */
@Service
@Transactional
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements DriverService {

    @Autowired
    private DriverMapper driverMapper;

    @Override
    public IPage<Driver> getDriverPage(Page<Driver> page, Integer driverStatus, Integer workStatus, 
                                      LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverPage(page, driverStatus, workStatus, startTime, endTime);
    }

    @Override
    public Driver getDriverByPhone(String phone) {
        return driverMapper.selectDriverByPhone(phone);
    }

    @Override
    public Driver getDriverByIdCard(String idCard) {
        return driverMapper.selectDriverByIdCard(idCard);
    }

    @Override
    public Driver getDriverByLicenseNo(String licenseNo) {
        return driverMapper.selectDriverByLicenseNo(licenseNo);
    }

    @Override
    public List<Driver> getOnlineDrivers(java.math.BigDecimal latitude, java.math.BigDecimal longitude, 
                                         java.math.BigDecimal radius, Integer limit) {
        return driverMapper.selectOnlineDrivers(latitude, longitude, radius, limit);
    }

    @Override
    public List<Driver> getAvailableDrivers(java.math.BigDecimal latitude, java.math.BigDecimal longitude, 
                                            java.math.BigDecimal radius, Integer limit) {
        return driverMapper.selectAvailableDrivers(latitude, longitude, radius, limit);
    }

    @Override
    public boolean createDriver(Driver driver) {
        driver.setCreateTime(LocalDateTime.now());
        driver.setUpdateTime(LocalDateTime.now());
        driver.setDriverStatus(1); // 默认状态：正常
        driver.setWorkStatus(0); // 默认工作状态：离线
        return save(driver);
    }

    @Override
    public boolean updateDriverStatus(Long driverId, Integer driverStatus, LocalDateTime updateTime) {
        return driverMapper.updateDriverStatus(driverId, driverStatus, updateTime) > 0;
    }

    @Override
    public boolean updateWorkStatus(Long driverId, Integer workStatus, LocalDateTime updateTime) {
        return driverMapper.updateWorkStatus(driverId, workStatus, updateTime) > 0;
    }

    @Override
    public boolean updateDriverLocation(Long driverId, java.math.BigDecimal latitude, 
                                       java.math.BigDecimal longitude, LocalDateTime updateTime) {
        return driverMapper.updateDriverLocation(driverId, latitude, longitude, updateTime) > 0;
    }

    @Override
    public boolean batchUpdateDriverStatus(List<Long> driverIds, Integer driverStatus, LocalDateTime updateTime) {
        return driverMapper.batchUpdateDriverStatus(driverIds, driverStatus, updateTime) > 0;
    }

    @Override
    public Long countDrivers(Integer driverStatus, Integer workStatus, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.countDrivers(driverStatus, workStatus, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> getDriverStatistics(LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverStatistics(startTime, endTime);
    }

    @Override
    public Map<String, Object> getDriverRatingStats(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverRatingStats(driverId, startTime, endTime);
    }

    @Override
    public Map<String, Object> getDriverIncomeStats(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverIncomeStats(driverId, startTime, endTime);
    }

    @Override
    public Map<String, Object> getDriverOrderStats(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverOrderStats(driverId, startTime, endTime);
    }

    @Override
    public Map<String, Object> getDriverWorkTimeStats(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverWorkTimeStats(driverId, startTime, endTime);
    }

    @Override
    public List<Driver> getRecentActiveDrivers(Integer hours, Integer limit) {
        return driverMapper.selectRecentActiveDrivers(hours, limit);
    }

    @Override
    public List<Map<String, Object>> getDriverRanking(Integer rankType, LocalDateTime startTime, 
                                                      LocalDateTime endTime, Integer limit) {
        return driverMapper.selectDriverRanking(rankType, startTime, endTime, limit);
    }

    @Override
    public List<Map<String, Object>> getSmartDriverRecommendation(Long orderId, java.math.BigDecimal pickupLatitude, 
                                                                  java.math.BigDecimal pickupLongitude, Integer limit) {
        return driverMapper.selectSmartDriverRecommendation(orderId, pickupLatitude, pickupLongitude, limit);
    }

    @Override
    public List<Map<String, Object>> getDriverTrajectory(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverTrajectory(driverId, startTime, endTime);
    }

    @Override
    public boolean updateDriverRating(Long driverId, java.math.BigDecimal rating, LocalDateTime updateTime) {
        return driverMapper.updateDriverRating(driverId, rating, updateTime) > 0;
    }

    @Override
    public boolean updateDriverIncome(Long driverId, java.math.BigDecimal income, LocalDateTime updateTime) {
        return driverMapper.updateDriverIncome(driverId, income, updateTime) > 0;
    }

    @Override
    public boolean updateDriverOrderCount(Long driverId, Integer orderCount, LocalDateTime updateTime) {
        return driverMapper.updateDriverOrderCount(driverId, orderCount, updateTime) > 0;
    }

    @Override
    public boolean updateDriverWorkHours(Long driverId, java.math.BigDecimal workHours, LocalDateTime updateTime) {
        return driverMapper.updateDriverWorkHours(driverId, workHours, updateTime) > 0;
    }

    @Override
    public boolean certifyDriver(Long driverId, Integer certType, Integer certStatus, LocalDateTime updateTime) {
        return driverMapper.certifyDriver(driverId, certType, certStatus, updateTime) > 0;
    }

    @Override
    public List<Driver> getPendingCertificationDrivers(Integer certType, Integer limit) {
        return driverMapper.selectPendingCertificationDrivers(certType, limit);
    }

    @Override
    public Map<String, Object> getDriverVehicleInfo(Long driverId) {
        return driverMapper.selectDriverVehicleInfo(driverId);
    }

    @Override
    public boolean updateDriverVehicleInfo(Long driverId, Map<String, Object> vehicleInfo, LocalDateTime updateTime) {
        return driverMapper.updateDriverVehicleInfo(driverId, vehicleInfo, updateTime) > 0;
    }

    @Override
    public List<Map<String, Object>> getDriverViolationRecords(Long driverId, LocalDateTime startTime, LocalDateTime endTime) {
        return driverMapper.selectDriverViolationRecords(driverId, startTime, endTime);
    }

    @Override
    public boolean addDriverViolationRecord(Long driverId, Integer violationType, String violationDesc, LocalDateTime createTime) {
        return driverMapper.insertDriverViolationRecord(driverId, violationType, violationDesc, createTime) > 0;
    }

}